

import 'package:bt_tool/basic/bt_controller.dart';
import 'package:bt_tool/basic/bt_date_tool.dart';
import 'package:bt_tool/basic/bt_db_tool.dart';
import 'package:bt_tool/basic/bt_language_tool.dart';
import 'package:bt_tool/basic/bt_model.dart';
import 'package:bt_tool/basic/bt_router.dart';
import 'package:bt_tool/widget/bt_dialog_widget.dart';
import 'package:bt_tool/widget/bt_text_field.dart';
import 'package:bt_tool/widget/bt_toast.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_demo/common/config/app_router.dart';
import 'package:flutter_demo/tools/project/project_define.dart';
import 'package:flutter_demo/tools/project/model/project_model.dart';
import 'package:flutter_demo/tools/project/project_tool.dart';
import 'package:get/get.dart';

class ProjectMainController extends BTController{

  var dataList = <ProjectModel>[].obs;
  var isSelectStyle = false;

  @override
  Future<void> initState() async {
    super.initState();
    isSelectStyle = routerJsonTool.getBool("isSelect");
    getData();
  }

  @override
  Future<void> getData() async {
    super.getData();
    var modelList = await ProjectTool.getProjectData();
    dataList.clear();
    dataList.addAll(modelList);
  }

  itemClick(int index){
    if(isSelectStyle){
      routerConfig?.commonCallbackAction(dataList[index],id: "didSelect");
    }
    BTRouter.share().pop();
  }

  createClick(){
    BTTextFieldController controller = BTTextFieldController();
    BTAlertInputWidget inputWidget = BTAlertInputWidget(
      controller: controller,isPcStyle: true,
      clickCallBack: (action) async {
        BTDialog.dismiss();
        if(action == BTAlertAction.ok){
          ProjectModel model = ProjectModel(null);
          model.name = controller.getText();
          if(model.name.isEmpty){
            return;
          }
          model.createTime = BTDateTool.currentTimeSeconds();
          await BTDbTool.share().insertData(ProjectDefine.tableName, model.convertToDictOnly(["name","createTime"]));
          BTToast.showToast("创建成功".btTrans);
          getData();
        }
      },
    );
    BTDialog.show(inputWidget);
  }

  showMenuView(ProjectModel model,Offset position) async {
    final overlay = Overlay.of(context).context.findRenderObject() as RenderBox;
    List<PopupMenuItem> items = [
      PopupMenuItem<ProjectMenuAction>(value: ProjectMenuAction.edit, child: Text("编辑".btTrans)),
      PopupMenuItem<ProjectMenuAction>(value: ProjectMenuAction.translate, child: Text("翻译".btTrans)),
      PopupMenuItem<ProjectMenuAction>(value: ProjectMenuAction.del, child: Text("删除".btTrans)),
    ];
    await showMenu(
      context: context,
      position: RelativeRect.fromRect(
        Rect.fromLTWH(position.dx, position.dy, 0, 0),
        Offset.zero & overlay.size,
      ),
      items: items,
    ).then((value) async {
      if (value == null) {
        return;
      }

      if(value == ProjectMenuAction.edit){
        BTTextFieldController controller = BTTextFieldController(
          textEditingController: TextEditingController(text: model.name)
        );
        BTAlertInputWidget inputWidget = BTAlertInputWidget(
          controller: controller,isPcStyle: true,
          clickCallBack: (action) async {
            BTDialog.dismiss();
            if(action == BTAlertAction.ok){
              String name = controller.getText();
              if(name.isEmpty){
                return;
              }
              model.name = name;
              await BTDbTool.share().update(ProjectDefine.tableName, model.convertToDictOnly(["name"]),where: "id = ?", whereArgs: [model.id]);
              dataList.refresh();
            }
          },
        );
        BTDialog.show(inputWidget);
        return;
      }

      if(value == ProjectMenuAction.del){
        await BTDbTool.share().deleteData(ProjectDefine.tableName,where: "id = ?",whereArgs: [model.id]);
        dataList.remove(model);
        return;
      }

      if(value == ProjectMenuAction.translate){
        BTRouter.share().pushToUrl(
            routerUrl: AppRouterDefine.translateMainPage,
            routerParameter: {
              "id" : model.id,
            }
        );
        return;
      }
    });
  }


}